#include <fstream>
#include <iostream>
#include <string>
#include <stdlib.h>
#include <dastrie.h>
#include <stdint.h>
#include <StringTokenizer.h>
#include <StringTrim.h>

using namespace std;

int main(int argc, char *argv[])
{
    uint32_t rec_cnt=0,i;
    int flag;
    string line;
    typedef dastrie::builder<std::string, std::string> builder_type;
    typedef dastrie::trie< std::string > trie_type;
    typedef builder_type::record_type record_type;

    vector< record_type > VRecords;
    record_type rec_tmp;
    
    if( argc != 3 )
    {
        cout << "./build_dict input_txt_name dict_name\n";
        exit(1);
    }
    ifstream fin ( argv[1] );

    if( fin == NULL)
    {
        cout <<  argv[1] <<" is NOT found!.\n";
        exit(1);
    }

    cout << "\nReading correct dict.\n";

    while( !fin.eof() )
    {
        rec_tmp.key = "";
        rec_tmp.value = "";

        getline( fin, line);

        if( line.length() <= 0) continue;
        if( line.substr(0,1) =="#") continue;
        
        flag = -1;
        flag = line.find("|",0);

        if( flag == -1) continue;

        rec_tmp.key     =   StringTrim::trim(   line.substr( 0, flag)   );
        rec_tmp.value   =   StringTrim::trim(   line.substr( flag+1, line.length() - flag+1) );


        cout << rec_tmp.key <<":"<<rec_tmp.value <<":\n";

        VRecords.push_back( rec_tmp );
        rec_cnt++;

        if( (rec_cnt % 1000 ) == 0) cout <<".";
    }
    cout << "\nBuilding dict.\n";
    
    try {
        builder_type builder;
        //builder.build(records, records + sizeof(records)/sizeof(records[0]));
        builder.build(& VRecords[0], &VRecords[  VRecords.size() -1 ]  ) ;

        std::ofstream ofs( argv[2]  , std::ios::binary);
        builder.write(ofs);
        ofs.close();
    } catch( const builder_type::exception& e) {
        std::cerr << "ERROR: " << e.what() << std::endl;
        return 1;
    }
    
    return 0;
}

